package net.thelostlands.plugins.thelostfortress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.InvalidConfigurationException;

/**
 *
 * @author faaabio
 */
public class AreaSet {

    TreeSet<Area> areas;

    AreaSet() {
        areas = new TreeSet<Area>();
    }

    public boolean insert(int x1, int x2, int y1, int y2, int z1, int z2) {

        Area a = new Area(x1, x2, y1, y2, z1, z2);
        return areas.add(a);
    }

    public boolean find(Location l) {
        return Main.mondo.equals(l.getWorld().getName()) && find(l.getBlockX(), l.getBlockY(), l.getBlockZ());
    }

    public void addChest(Location l) {
        Area a = getArea(l);
        if (a != null) {
            a.addChest(l);
        }
    }

    public void removeChest(Location l) {
        Area a = getArea(l);
        if (a != null) {
            a.breakChest(l);
        }
    }

    public int openChest(Location l, String player) {//0 nulla di cambiato, 1 forte conquistato, 2 forte deconqusitato

        Area a = getArea(l);
        if (a == null) {
            return 0;
        }
        String wasConquered = a.isFortressTaken();
        a.openChest(l, player);
        String isConquered = a.isFortressTaken();
        if (wasConquered != null && isConquered == null) {
            return 2;
        } else if (wasConquered == null && isConquered != null) {
            return 1;
        } else {
            return 0;
        }
    }

    public void clear() {
        if (areas == null || areas.isEmpty()) {
            return;
        }
        for (Area a : areas) {
            a.clear();
        }

    }

    public boolean find(int x, int y, int z) {//O(n)       
        if (areas == null || areas.isEmpty()) {
            return false;
        }
        for (Area a : areas) {
            if (a.x1 > x) {
                return false;
            }
            if ((a.isIn(x, y, z))) {
                return true;
            }
        }
        return false;
    }

    public String propertyOf(Location l) {
        if (areas == null || areas.isEmpty()) {
            return null;
        }
        Area a = getArea(l);
        if (a == null) {
            return null;
        }
        return a.isFortressTaken();
    }

    private Area getArea(Location l) {
        if (areas == null || areas.isEmpty()) {
            return null;
        }
        int x = l.getBlockX();
        int y = l.getBlockY();
        int z = l.getBlockZ();
        for (Area a : areas) {
            if (a.x1 > x) {
                return null;
            }
            if (a.isIn(x, y, z)) {
                return a;
            }
        }
        return null;
    }

    public boolean creeperFind(int x, int y, int z) {
        if (areas == null || areas.isEmpty()) {
            return false;
        }
        for (Area a : areas) {
            if (a.x1 > x + 5) {
                return false;
            }
            if (a.isCreeperIn(x, y, z)) {
                return true;
            }
        }
        return false;
    }

    public Area find(Block b) {
        if (areas == null || areas.isEmpty()) {
            return null;
        }
        for (Area a : areas) {
            if (a.x1 > b.getX()) {
                return null;
            }
            if (a.isIn(b)) {
                return a;
            }
        }
        return null;
    }

    boolean findChest(Location location) {
        if (areas == null || areas.isEmpty()) {
            return false;
        }
        Area a = find(location.getBlock());
        if (a == null) {
            return false;
        }
        return a.findChest(location);
    }

    public boolean remove(int x1, int x2, int y1, int y2, int z1, int z2) {
        return areas.remove(findArea(x1, x2, y1, y2, z1, z2));
    }

    public boolean remove(Area a) {
        if (a == null) {
            return false;
        }
        this.areas.remove(a);
        a.remove();
        return true;
    }

    private Area findArea(int x1, int x2, int y1, int y2, int z1, int z2) {
        if (areas == null || areas.isEmpty()) {
            return null;
        }
        for (Area a : areas) {
            if (a.x1 > x1) {
                return null;
            }
            if (a.uguale(x1, x2, y1, y2, z1, z2)) {
                return a;
            }
        }
        return null;
    }

    /*
     * public void load() {
     *
     * File[] fL = new File(Main.dataFile).listFiles(); File f = null; if (fL ==
     * null || fL.length < 1) { Main.log.severe("[The Lost Fortress] Problemi
     * nel caricamento delle fortezze"); return;
     *
     * }
     * for (File x : fL) { if (x.getName().equals(Main.mondo)) { f = x; } } if
     * (f == null) { Main.log.severe("[The Lost Fortress] Problemi nel
     * caricamento delle fortezze"); return; } try { Scanner scanner = new
     * Scanner(f); while (scanner.hasNext()) { int x1 = scanner.nextInt();
     *
     * int x2 = scanner.nextInt(); int y1 = scanner.nextInt(); int y2 =
     * scanner.nextInt(); int z1 = scanner.nextInt(); int z2 =
     * scanner.nextInt(); Main.area.insert(x1, x2, y1, y2, z1, z2);
     * scanner.next(); } } catch (IOException ex) { Main.log.severe("[The Lost
     * Fortress] Problemi nel caricamento delle fortezze"); return; }
     * Main.log.severe("[The Lost Fortress] Fortezze caricate con successo."); }
     */
    /*
     * public void store() { File f = new File(Main.dataFile + File.separator +
     * Main.mondo); FileOutputStream fw = null; try { fw = new
     * FileOutputStream(f); for (Area a : areas) {
     * fw.write(a.toString().getBytes()); } fw.close(); } catch (IOException ex)
     * { Logger.getLogger(AreaSet.class.getName()).log(Level.SEVERE, null, ex);
     * }
     *
     * }
     */
    public void store() {
        for (Area a : areas) {
            Main.log.info(a.toStringConfig());
        }
    }

    public void load() {
        File[] fL = new File(Main.dataFile).listFiles();
        if (fL == null || fL.length < 1) {
            Main.log.severe("[The Lost Fortress] Nessuna fortezza caricata");
            return;

        }
        for (File x : fL) {
            if (!x.getName().equals("config.yml")) {
                try {
                    Area a = new Area(x);
                    areas.add(a);
                    Main.log.info("[TheLostFortress] Caricata: " + a.toString());
                } catch (IOException ex) {
                    Main.log.severe("[The Lost Fortress] Problemi nel caricamento delle fortezze" + ex);

                } catch (InvalidConfigurationException ex) {
                    Main.log.severe("[The Lost Fortress] Problemi nel caricamento delle fortezze" + ex);
                }
            }
        }
    }
}
